define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/index' + location.search,
                    add_url: 'order/add',
                    del_url: 'order/del',
                    multi_url: 'order/multi',
                    table: 'order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'order_no', title: __('Order_no')},
                        {field: 'user.nickname', title: __('User_id')},
                        {field: 'spec_type', title: __('Spec_type'), searchList: {"10":__('Spec_type 10'),"20":__('Spec_type 20'),"30":__('Spec_type 30')}, formatter: Table.api.formatter.normal},
                        {
                            field: 'goods_name', title: __('Goods_name'),formatter:function(value,row,index){
                               
                               if(row.goods_name){
                                    return row.goods_name;
                               }else{
                                    var goods_name = '';
                                    for (var i = row.goods_list.length - 1; i >= 0; i--) {
                                        goods_name += row.goods_list[i]['goods_name']+'<br><br>';
                                    }
                                    return goods_name;
                               }

                            }
                        },

                        // {field: 'cover', title:'商品封面', events: Table.api.events.image, formatter: Table.api.formatter.images},
                        {
                            field: 'cover', title:'商品封面',formatter:function(value,row,index){
                               
                               if(row.goods_name){
                                    return "<a href='javascript:'><img class='img-sm img-center' src='"+value+"'></a>"
                               }else{
                                    var goods_name = '';
                                    for (var i = row.goods_list.length - 1; i >= 0; i--) {
                                        goods_name += "<a href='javascript:'><img class='img-sm img-center' src='"+row.goods_list[i]['cover']+"'></a><br><br>";
                                    }
                                    return goods_name;
                               }

                            }

                        },

                        {
                            field: 'goods_attr', title: __('Goods_attr'),formatter:function(value,row,index){
                               
                               if(row.goods_name){
                                    return row.goods_attr;
                               }else{
                                    var goods_name = '';
                                    for (var i = row.goods_list.length - 1; i >= 0; i--) {
                                        goods_name += row.goods_list[i]['goods_attr']+'<br><br>';
                                    }
                                    return goods_name;
                               }

                            }
                        },
                        {
                            field: 'num', title: __('Num'),formatter:function(value,row,index){
                               
                               if(row.goods_name){
                                    return row.num;
                               }else{
                                    var goods_name = '';
                                    for (var i = row.goods_list.length - 1; i >= 0; i--) {
                                        goods_name += row.goods_list[i]['num']+'<br><br>';
                                    }
                                    return goods_name;
                               }

                            }
                        },
                        {
                            field: 'unit_price', title: __('Unit_price'),formatter:function(value,row,index){
                               
                               if(row.goods_name){
                                    return row.unit_price;
                               }else{
                                    var goods_name = '';
                                    for (var i = row.goods_list.length - 1; i >= 0; i--) {
                                        goods_name += row.goods_list[i]['unit_price']+'<br><br>';
                                    }
                                    return goods_name;
                               }

                            }
                        },
                        {field: 'total_price', title: __('Total_price'), operate:'BETWEEN'},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        
                        {field: 'order_status', title: __('Order_status'),
                             operate:false,formatter:function(value){
                                if(value==10){
                                    return '<span style="color:red">未支付</span>';
                                }
                                if(value==20){
                                    return '<span style="color:grown">已支付待发货</span>';
                                }
                                if(value==30){
                                    return '<span style="color:green">已发货</span>';
                                }
                                if(value==40){
                                    return '<span style="color:green">已确认收货</span>';
                                }
                                if(value==50){
                                    return '<span style="color:green">已还机</span>';
                                }
                             }
                         },

                        {
                            field: 'buttons',
                            width: "120px",
                            title: '查看详情',
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: __('查看详情'),
                                    title: __('查看详情'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-eye',
                                    url: 'order/fahuo',
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return true;
                                    }
                                },
                                
                            ],
                            formatter: Table.api.formatter.buttons
                        },
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });
            
            // 为表格绑定事件
            Table.api.bindevent(table);

            // 绑定TAB事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var that = $(this);
                var options = table.bootstrapTable('getOptions');
                options.pageNumber = 1;
                options.queryParams = function (params) {
                    var filter = {};
                    filter['order_status'] = that.data("order_status");
                    params.filter = JSON.stringify(filter);
                    return params;
                };

                table.bootstrapTable('refresh', {});
                return false;
            });

        },
        add: function () {
            Controller.api.bindevent();
        },
  
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});